package com.springcloud.apigateway;

import com.netflix.zuul.FilterProcessor;
import com.springcloud.apigateway.filter.AccessFilter;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cloud.client.SpringCloudApplication;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
import org.springframework.cloud.netflix.zuul.filters.ZuulProperties;
import org.springframework.cloud.netflix.zuul.filters.discovery.PatternServiceRouteMapper;
import org.springframework.context.annotation.Bean;

@EnableZuulProxy
@SpringCloudApplication
public class ApiGatewayApplication {

	@Bean
	public AccessFilter accessFilter(){
		return new AccessFilter();
	}

	/**
	 * 自定义路径映射规则
	 * 将serviceId-v1映射为/v1/serviceId
	 * @return
	 */
	@Bean
	public PatternServiceRouteMapper routeMapper(){
		return new PatternServiceRouteMapper(
				"(?<name>^.+)-(?<version>v.+$)","${version}/${name}");
	}

	/**
	 * 将zuul的配置动态化
	 * @return
	 */
	@Bean
	@RefreshScope
	@ConfigurationProperties("zuul")
	public ZuulProperties zuulProperties() {
		return new ZuulProperties();
	}

	public static void main(String[] args) {
//		FilterProcessor.setProcessor(new ErrorFilterProcessor());
		SpringApplication.run(ApiGatewayApplication.class, args);
	}
}
